package com.e9ab98e991ab.libcommon.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;

import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;

public class BlurBitmapUtil {
    private static BlurBitmapUtil sInstance;
    private BlurBitmapUtil() {
    }
    public static BlurBitmapUtil instance() {
        if (sInstance == null) {
            synchronized (BlurBitmapUtil.class) {
                if (sInstance == null) {
                    sInstance = new BlurBitmapUtil();
                }
            }
        }
        return sInstance;
    }
    /**
     * @param context   上下文对象
     * @param image     需要模糊的图片
     * @param outWidth  输入出的宽度
     * @param outHeight 输出的高度
     * @return 模糊处理后的Bitmap
     */
    public Bitmap blurBitmap(Context context, BitmapPool pool, Bitmap image, int outWidth, int outHeight, int blurRadius, int sampling) {
        int scaledWidth = outWidth / sampling;
        int scaledHeight = outHeight / sampling;


        //将缩小后的图片做为预渲染的图片
        Bitmap inputBitmap = Bitmap.createScaledBitmap(image, scaledWidth, scaledHeight, false);
        // 创建一张渲染后的输出图片
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
        // 创建RenderScript内核对象
        RenderScript rs = RenderScript.create(context);
        // 创建一个模糊效果的RenderScript的工具对象
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        // 由于RenderScript并没有使用VM来分配内存,所以需要使用Allocation类来创建和分配内存空间
        // 创建Allocation对象的时候其实内存是空的,需要使用copyTo()将数据填充进去
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        // 设置渲染的模糊程度, 25f是最大模糊度
        blurScript.setRadius(blurRadius);
        // 设置blurScript对象的输入内存
        blurScript.setInput(tmpIn);
        // 将输出数据保存到输出内存中
        blurScript.forEach(tmpOut);
        // 将数据填充到Allocation中
        tmpOut.copyTo(outputBitmap);






//        Bitmap bitmap = pool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
//
//        Canvas canvas = new Canvas(bitmap);
//        canvas.scale(1 /  sampling, 1 /  sampling);
//        Paint paint = new Paint();
//        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
//        canvas.drawBitmap(image, 0, 0, paint);
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//            try {
//                bitmap = RSBlur.blur(context, bitmap, blurRadius);
//            } catch (RSRuntimeException e) {
//                bitmap = FastBlur.blur(bitmap, blurRadius, true);
//            }
//        } else {
//            bitmap = FastBlur.blur(bitmap, blurRadius, true);
//        }


        return outputBitmap;
    }
}

